package com.kobeliu.netty.mq;

import com.rabbitmq.client.*;
import org.bouncycastle.jcajce.provider.asymmetric.COMPOSITE;
import org.checkerframework.checker.units.qual.C;

import java.util.ArrayList;
import java.util.List;

public class MessageMQUtils {

    private final List<Connection> connections = new ArrayList<>();

    private final int MAX_CONNECTION = 20;

    //定义交换机
    public static final String EXCHANGE_MSG = "exchange_chat_msg";
    //定义队列
    public static final String QUEUE_MSG = "queue_chat_msg";
    //定义路由key
    public static final String SAVE_MSG_ROUTING_KEY = "imooc.chat.msg.save.do";

    //本地
//    private final String HOST ="192.168.10.70";
    //线上
    private final String HOST ="192.168.6.83";
    private final Integer PORT =5672;
    private final String VIRTUALHOST ="/";
    private final String USERNAME ="kobeliu";
    private final String PASSWORD ="88702611";

    public ConnectionFactory factory;

    public MessageMQUtils() {
        getRabbitMqFactory();
    }

    public ConnectionFactory getRabbitMqFactory(){
        return getFactory();
    }

    public ConnectionFactory getFactory(){
        initFactory();
        return factory;
    }

    private void initFactory(){
        try{
            if(factory == null){
                factory = new ConnectionFactory();
                factory.setHost(HOST);
                factory.setPort(PORT);
                factory.setVirtualHost(VIRTUALHOST);
                factory.setUsername(USERNAME);
                factory.setPassword(PASSWORD);
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void sendMsg(String msg,String queue) throws Exception {
        Connection connection = getConnection();
        Channel channel = connection.createChannel();
        channel.basicPublish("",queue, MessageProperties.PERSISTENT_TEXT_PLAIN,msg.getBytes());

        channel.close();
        setConnection(connection);
    }

    public void sendMsg(String msg,String exchange,String routingKey) throws Exception {
        Connection connection = getConnection();
        Channel channel = connection.createChannel();
        channel.basicPublish(exchange,routingKey,MessageProperties.PERSISTENT_TEXT_PLAIN,msg.getBytes());
        channel.close();
        setConnection(connection);
    }

    public GetResponse basicGet(String queue,boolean autoAck) throws Exception {
        GetResponse getResponse = null;
        Connection connection = getConnection();
        Channel channel = connection.createChannel();
        getResponse = channel.basicGet(queue,autoAck);
        channel.close();
        setConnection(connection);
        return getResponse;
    }

    public Connection getConnection() throws Exception {
        return getAndSetConnection(true,null);
    }

    public void setConnection(Connection connection) throws Exception {
        getAndSetConnection(false,connection);
    }

    public synchronized Connection getAndSetConnection(boolean isGet, Connection connection) throws Exception {
        if(isGet){
            if(connections.isEmpty()){
                return factory.newConnection();
            }
            Connection newConnection = connections.get(0);
            connections.remove(0);
            if(newConnection.isOpen()){
                return newConnection;
            }else {
                return factory.newConnection();
            }
        }else{
            if(connections.size()<MAX_CONNECTION){
                connections.add(connection);
            }
            return null;
        }
    }



}
